<?php

namespace app\admin\controller\business;

use app\common\controller\Backend;
use think\Db;

/**
 * 客户管理
 *
 * @icon fa fa-circle-o
 */
class Privatesea extends Backend
{
    //修改是否开启关联查询属性
    protected $relationSearch = true;

    //设置数据控制的关联字段
    protected $dataLimitField = 'adminid';

    //设置看到数据的权限
    protected $dataLimit = 'personal';



    /**
     * Privatesea模型对象
     * @var \app\admin\model\business\Privatesea
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Business.Business');
        $this->view->assign("genderList", $this->model->getGenderList());
        $this->view->assign("dealList", $this->model->getDealList());
        $this->view->assign("authList", $this->model->getAuthList());
    }

    public function index()
    {
        $this->request->filter(['strip_tags', 'trim']);

        if($this->request->isAjax())
        {
            //Backend 后台基类控制器中提供的一个获取查询条件的参数方法
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $AuthWhere = ['business.adminid' => ['exp',Db::raw('is not null')]];

            //查询出总条数 和 每页的数据量
            $total = $this->model
                ->with(['source', 'admin','parent', 'provinces', 'citys', 'districts'])
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();  //查询总数

            $list = $this->model
                ->with(['source', 'admin','parent', 'provinces', 'citys', 'districts'])
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();  //查询数据

            //返回的数据 下方的这两个参数名字是固定，这个是前台插件(boostrap-table这个插件的固定的)
            $result = ['total' => $total, 'rows' => $list];

            //返回json类型
            return json($result);
        }

        return $this->view->fetch();
    }

    //回收站方法
    public function recyclebin()
    {
        $this->request->filter(['strip_tags', 'trim']);

        if($this->request->isAjax())
        {
            //Backend 后台基类控制器中提供的一个获取查询条件的参数方法
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $AuthWhere = ['business.adminid' => ['exp',Db::raw('is not null')]];

            //查询出总条数 和 每页的数据量
            $total = $this->model
                ->onlyTrashed()  //只查询软删除的数据
                ->with(['source', 'admin', 'provinces', 'citys', 'districts'])
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();  //查询总数

            $list = $this->model
                ->onlyTrashed() //只查询软删除的数据
                ->with(['source', 'admin', 'provinces', 'citys', 'districts'])
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();  //查询数据

            //返回的数据 下方的这两个参数名字是固定，这个是前台插件(boostrap-table这个插件的固定的)
            $result = ['total' => $total, 'rows' => $list];

            //返回json类型
            return json($result);
        }
    }


    //恢复的方法
    public function restore($ids = NULL)
    {
        //恢复指定的ID数据
        $result = $this->model->restore(['id' => ['in', $ids]]);

        if($result === FALSE)
        {
            $this->error('恢复失败');
            exit;
        }else
        {
            $this->success('恢复成功');
            exit;
        }
    }

    //真实删除
    public function destroy($ids = NULL)
    {
        $rows = $this->model->withTrashed()->where(['id' => ['in', $ids]])->field('avatar,poster')->select();

        if(!$rows)
        {
            $this->error('暂无销毁数据');
            exit;
        }

        $avatar = array_column($rows, 'avatar');
        $avatar = array_filter($avatar);
        $avatar = array_unique($avatar);

        $poster = array_column($rows, 'poster');
        $poster = array_filter($poster);
        $poster = array_unique($poster);

        //真实删除
        $result = $this->model->withTrashed()->whereIn('id', $ids)->delete(true);

        if($result === FALSE)
        {
            $this->error('销毁失败');
            exit;
        }else
        {
            //删除头像和海报
            foreach($avatar as $item)
            {
                is_file(".".$item) && @unlink(".".$item);
            }

            foreach($poster as $item)
            {
                is_file(".".$item) && @unlink(".".$item);
            }

            $this->success('销毁成功');
            exit;
        }
    }


}
